import { _decorator, Animation, AnimationClip, AnimationState } from "cc";
import State from "./Base/State";
import StateMachine, { getInitParamsTrigger } from "./Base/StateMachine";

import { PlayerManager } from "./PlayerManager";
import { EState, EntityType, EParamsName } from "./Config/Enum";
import { PlayerConfig } from "./Config/PlayerConfig";
const { ccclass } = _decorator;

@ccclass("PlayerStateMachine")
export class PlayerStateMachine extends StateMachine {

  animationSpeed = 0.5
  attackSpeed

//initState 只用于显示动画
 init(type: EntityType, attackSpeed: number, initState: boolean = false) {
    this.type = type;
    this.animationComponent = this.node.addComponent(Animation);
    this.attackSpeed = attackSpeed



    if (initState) {
      this.initParams_initStateOnly();
      this.initStateMachines_initStateOnly();
      this.initAnimationEvent();
          this.currentState = this.stateMachines.get(EParamsName.IdleFront)
    }else{
      this.initParams();
      this.initStateMachines();
      this.initAnimationEvent();
    }







  }

  // private async loadStates() {
  //   const promises: Promise<void>[] = [];
  //   this.stateMachines.forEach(state => {
  //     promises.push(state.load());
  //   });
  //   await Promise.all(promises);
  // }

  initParams() {
    this.params.set(EParamsName.Idle, getInitParamsTrigger());
    this.params.set(EParamsName.Attack, getInitParamsTrigger());
    this.params.set(EParamsName.IdleFront, getInitParamsTrigger());
  }
  initParams_initStateOnly() {
    this.params.set(EParamsName.IdleFront, getInitParamsTrigger());
  }

  initStateMachines() {
    this.stateMachines.set(EParamsName.IdleFront, new State(this, `${this.type}${EState.IdleFront}`, AnimationClip.WrapMode.Loop,false, this.animationSpeed));
    // console.log("type attackspeed", type, this.attackSpeed) //这里打印是0.99
    this.stateMachines.set(EParamsName.Idle, new State(this, `${this.type}${EState.Idle}`, AnimationClip.WrapMode.Loop,false, this.animationSpeed));
    this.stateMachines.set(EParamsName.Attack, new State(this, `${this.type}${EState.Attack}`,   AnimationClip.WrapMode.Normal,false, this.attackSpeed));
  }

  
  initStateMachines_initStateOnly() {
    this.stateMachines.set(EParamsName.IdleFront, new State(this, `${this.type}${EState.IdleFront}`, AnimationClip.WrapMode.Loop,false, this.animationSpeed));
  }

  // initAnimationEvent() {}

  initAnimationEvent() {
    this.animationComponent.on(Animation.EventType.FINISHED, (type, state) => {
        this.onAnimationFinished(type, state);
    }, this);
}


  onAnimationFinished(type: Animation.EventType, state: AnimationState) {
    if (state.name.includes(EState.Die)) {
      // 死亡动画播放完毕，触发销毁逻辑
      this.node.emit("PlayerDieAnimationFinished");
    }else if (state.name.includes(EState.Attack)) {
      // 攻击动画播放完毕，触发攻击逻辑
      this.node.emit("PlayerAttackAnimationFinished");
    }
  }

// 修改 PlayerStateMachine 的 run 方法
run() {
    
    // 优先处理 null 状态
    if (this.currentState === null) {

        this.currentState = this.stateMachines.get(EParamsName.Idle);


        
    }

    switch (this.currentState) {
        case this.stateMachines.get(EParamsName.Idle):
            if (this.params.get(EParamsName.Attack).value) {
                this.currentState = this.stateMachines.get(EParamsName.Attack);
            } else if (this.params.get(EParamsName.IdleFront).value) {
                this.currentState = this.stateMachines.get(EParamsName.IdleFront);
            }
            break;

        case this.stateMachines.get(EParamsName.Attack):
            if (this.params.get(EParamsName.Idle).value) {
                this.currentState = this.stateMachines.get(EParamsName.Idle);
            } else if (this.params.get(EParamsName.IdleFront).value) {
                this.currentState = this.stateMachines.get(EParamsName.IdleFront);
            }
            break;

        case this.stateMachines.get(EParamsName.IdleFront):
            // if (this.params.get(EParamsName.Attack).value) {
            //     this.currentState = this.stateMachines.get(EParamsName.Attack);
            // } else if (this.params.get(EParamsName.Idle).value) {
            //     this.currentState = this.stateMachines.get(EParamsName.Idle);
            // }
            break;

        default:
            // 确保所有状态都有处理
            this.currentState = this.stateMachines.get(EParamsName.Idle);
            break;
    }
}
}
